package com.mybindview.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.mybindview.HandlerClick.TextViewClick;
import com.mybindview.R;
import com.mybindview.adapter.CommonAdapter;
import com.mybindview.bean.ItemBean;
import com.mybindview.bean.UserBean;
import com.mybindview.databinding.ActivityMainBinding;
import com.mybindview.databinding.ItemListBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private CommonAdapter adapter;
    private ActivityMainBinding binder;
    private List<Object> datalist=new ArrayList<Object>();
    private ItemBean bean;
    private LayoutInflater inflate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binder=DataBindingUtil.setContentView(this, R.layout.activity_main);
        UserBean user=new UserBean();
        user.setName("名字");
        user.setAge(2017);
        user.setAddress("11");
        binder.setUser(user);
        user.setAddress("地址");
        binder.txt.setText("niaishda");

        //binder.setIsShow(false);

        for(int i=0;i<20;i++){
            bean=new ItemBean();
            bean.setContent("-->"+i);
            datalist.add(bean);
        }

        inflate=LayoutInflater.from(this);

        adapter=new CommonAdapter(datalist) {
            @Override
            public View bindViewItem(int position, View view, ViewGroup viewGroup, Object obj) {
                ItemListBinding binding=null;
                if(view==null){
                    View v=inflate.inflate(R.layout.item_list,null);
                    //binding = DataBindingUtil.inflate(inflate,R.layout.item_list,viewGroup,false);
                    binding=DataBindingUtil.bind(v);
                    view=binding.getRoot();
                }else{
                    binding=DataBindingUtil.getBinding(view);
                }
                binding.setIsEnable(true);
                binding.setItem((ItemBean)obj);
                binding.setContent(new HandlerClick());
                return view;
            }
        };
        binder.listview.setAdapter(adapter);
        //binder.setVariable(com.mybindview.BR.viewClick,new TextViewClick());
        binder.setViewClick(new TextViewClick());
        binder.setBtnClick(new HandlerClick());
        binder.setIsColor(false);
        //binder.setViewutil(new ViewUtil());

        //binder.getViewutil().color=this.getResources().getColor(R.color.colorPrimary);
    }

    public class HandlerClick{
        public void txtClick(View view){
            //Log.e("Mytext","MainActivity--->onclick");
            Toast.makeText(MainActivity.this,"MainActivity--->onclick",Toast.LENGTH_SHORT).show();
        }
        public void btnClick(View view){
            switch(view.getId()){
                case R.id.btn:
                    Toast.makeText(MainActivity.this,"MainActivity--->btnClick",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
}
